package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.ChapterBean;
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.SettingManager;

import java.util.List;

/**
 * Created by wcy on 2017\7\4 0004.
 */

public class ChapterListAdapter extends BaseQuickAdapter<ChapterBean> {
    private String mCurOrder;
    int mNormalColor;
    int mReadedColor;
    public ChapterListAdapter(Context context) {
        super(context);
    }

    public ChapterListAdapter(Context context, List<ChapterBean> data, String bookId) {
        super(context, data);
        mCurOrder = String.valueOf(SettingManager.getInstance().getCurReadProgress(bookId).chapter);
        mNormalColor = mContext.getResources().getColor(R.color.color666);
        mReadedColor = Color.parseColor("#ff5a4d");
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_dir;
    }

    @Override
    protected void convert(BaseViewHolder holder, ChapterBean item, int position) {
        holder.setText(R.id.tv_dir_name, item.title);
        TextView tv = holder.getView(R.id.iv_dir_flag);
        int resId;
        if (item.price != 0) {
            //resId = R.mipmap.ic_lock_tag;
            resId = 0;
        } else {
            resId = 0;
        }
        tv.setBackgroundResource(resId);
        DisplayUtils.visible(holder.getView(R.id.divider));
        if (TextUtils.equals(item.order, mCurOrder)) {
            holder.setTextColor(R.id.tv_dir_name, mReadedColor);
        } else {
            holder.setTextColor(R.id.tv_dir_name, mNormalColor);
        }
    }

    public int getReadPosition (){
       int postion = 0;
       boolean include = false;
        for (ChapterBean item: mData ) {
            if (TextUtils.equals(item.order, mCurOrder)) {
                include = true;
                break;
            }
            postion++;
        }
        if(!include){
            postion = 0;
        }
       postion += getHeaderViewsCount();
       return postion;
    }
}
